package com.example.janlay.test;

import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.graphics.drawable.Drawable;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.widget.ImageView;
import android.widget.TextView;

public class activity_install_success extends AppCompatActivity {

    private ImageView appIconView;

    private TextView appNameView;

    private TextView appVersionView;

    private String appUrl;

    private static final String TAG = "activity_install_succes";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_install_success);
        try {
            appIconView = findViewById(R.id.appIcon);
            appNameView = findViewById(R.id.appName);
            appVersionView = findViewById(R.id.appVersion);
        } catch (Exception e) {
            Log.e(TAG, "onCreate: ", e);
        }
        appUrl = getIntent().getStringExtra("appUrl");
        setApkInfo(appUrl, this);
    }

    /**
     * 获取apk包的信息：版本号，名称，图标等
     *
     * @param absPath apk包的绝对路径
     * @param context
     */
    public void setApkInfo(String absPath, Context context) {
        PackageManager pm = context.getPackageManager();
        PackageInfo pkgInfo = pm.getPackageArchiveInfo(absPath, PackageManager.GET_ACTIVITIES);
        if (pkgInfo != null) {
            ApplicationInfo appInfo = pkgInfo.applicationInfo;
            /* 必须加这两句，不然下面icon获取是default icon而不是应用包的icon */
            appInfo.sourceDir = absPath;
            appInfo.publicSourceDir = absPath;
            String appName = pm.getApplicationLabel(appInfo).toString();// 得到应用名
            String packageName = appInfo.packageName; // 得到包名
            String version = pkgInfo.versionName; // 得到版本信息
            /* icon1和icon2其实是一样的 */
            Drawable icon1 = pm.getApplicationIcon(appInfo);// 得到图标信息
            appNameView.setText(appName);
            appVersionView.setText(version);
            appIconView.setImageDrawable(icon1);
        }
    }
}
